<?php

require_once 'constants.php';

// returns array or false
// 'id': int
// 'username': string
// 'first_name': string
// 'last_name': string
// ... maybe whatever else will be needed
function auth($username, $password) {
    global $passwd_file;
    $contents = file_get_contents($passwd_file);
    // TODO: get rid of regexps at all
    $lines = preg_split('/[\r\n]+/', $contents);
    $hashed_password = hash_password($password);
    foreach ($lines as $line) {
        $array = preg_split('/:/', $line);
        if (count($array) >= 5 && $array[1] == $username && $array[4] == $hashed_password) {
            $answer = array();
            $answer['id'] = (int)$array[0];
            $answer['username'] = $username;
            $answer['first_name'] = $array[2];
            $answer['last_name'] = $array[3];
            return $answer;
        }
    }
    return false;
}

function username_exists($username) {
    global $passwd_file;
    $s = file_get_contents($passwd_file);
    $n = strlen($s);
    $m = strlen($username);
    for ($i = 0; $i < $n; ) {
        $j = $i;
        while ($j < $n && $s[$j] != ':') $j++;
        $i = $j + 1;
        $j = $i;
        while ($j < $n && $s[$j] != ':') $j++;
        if ($j - $i == $m) {
            $equals = true;
            for ($k = $i; $k < $j; $k++) if (tolower($s[$k]) != tolower($username[$k - $i])) {
                $equals = false;
                break;
            }
            if ($equals) return true;
        }
        $i = $j;
        while ($i < $n && $s[$i] != "\r" && $s[$i] != "\n") $i++;
        while ($i < $n && ($s[$i] == "\r" || $s[$i] == "\n")) $i++;
    }
    return false;
}

function create_new_user($username, $password, $firstname, $lastname) {
    global $passwd_file, $data_dir;
    while (!rename($passwd_file, $passwd_file.".tmp")) usleep(200000);
    $contents = file_get_contents($passwd_file.".tmp");
    $lines = preg_split('/[\r\n]+/', $contents);
    $id = 1;
    foreach ($lines as $line) {
        $array = preg_split('/:/', $line);
        if (count($array) >= 5 && $array[0] == $id) {
            $id++;
        }
    }
    $hashed_password = hash_password($password);
    $new_line = "$id:$username:$firstname:$lastname:$hashed_password:";
    file_put_contents($passwd_file.".tmp", $contents.$new_line."\n");
    mkdir($data_dir.$id, 0777, true);
    rename($passwd_file.".tmp", $passwd_file);
    return auth($username, $password);
}

function load_settings($user) {
    global $data_dir, $settings_file, $default_settings;
    $filename = $data_dir.$user['id'].'/'.$settings_file;
    if (!file_exists($filename)) {
        return $default_settings;
    }
    $contents = file_get_contents($filename);
    $lines = preg_split('/[\r\n]+/', $contents);
    $answer = $default_settings;
    foreach ($lines as $line) {
        $x = strpos($line, '=');
        $key = trim(substr($line, 0, $x));
        $value = trim(substr($line, $x + 1));
        $answer[$key] = $value;
    }
    return $answer;
}

function save_settings($user, $settings) {
    global $data_dir, $settings_file, $trash_file, $default_settings;
    $filename = $data_dir.$user['id'].'/'.$settings_file;
    if ($settings == $default_settings) {
        if (file_exists($filename)) rename($filename, $trash_file);
        return;
    }
    $contents = "";
    foreach ($settings as $key => $value) {
        if (isset($default_settings[$key]) && $default_settings[$key] != $value)
            $contents = $contents.$key.'='.$value."\n";
    }
    file_put_contents($filename, $contents);
}

?>
